import {
    GetCateDetail,
    GetRecommendCase,
    SetExamcaseView,
    SetTaskScore,
} from "~/api";

export const useContent = () => {
    const dataCaseList = ref([])
    const CaseListDialogRef = ref(null)
    const CaseContactDialogRef = ref(null);
    const CaseMenusDialogRef = ref(null);
    const pageStore = usePageStore();
    const userStore = useUserStore();
    const router = useRouter();
    const route = useRoute();
    const id = route.params.id;
    const detail = ref({});
    const recommend = ref([]);
    const isLoginTipsDialogVisible = ref(false);

    const onCaseView = (key) => {
        if (detail.value[key]) {
            if (userStore.isLogin) {
                navigateTo("/caseView/" + id + ".html?key=" + key, {
                    open: "_blank",
                });
            } else {
                LoginModal(
                    true,
                    {
                        url: "/caseView/" + id + ".html?key=" + key,
                    },
                    {},
                    true
                );
            }
        }
    };

    const onCaseExperience = () => {
        if (userStore.isLogin) {
            // 多级-2层
            if (detail.value.is_many == '2') {
                dataCaseList.value = detail.value.exp_url
                return CaseListDialogRef.value.show()
            }

            if (id == 90) {
                CaseMenusDialogRef.value.show(id);
            } else if (id == 264) {
                if (userStore.userInfo.level == 3) {
                    CaseMenusDialogRef.value.show(id);
                } else {
                    CaseContactDialogRef.value.show();
                }
            } else if (id == 165) {
                if ([
                    "13621984347",
                    "13601086547",
                    "18406502298",
                    "13068080889",
                    "18301404307",
                    "15210763667",
                    "13521207001",
                    "13035861988",
                    "18027651022"
                ].includes(userStore.userInfo.mobile)) {
                    CaseMenusDialogRef.value.show(id);
                } else {
                    CaseContactDialogRef.value.show();
                }
            } else if (id == 76 || id == 253 || id == 256 || id == 45) {

                if (userStore.userInfo.level == 3 && detail.value.exp_url) {
                    window.open(detail.value.exp_url);
                } else {
                    CaseContactDialogRef.value.show();
                }
            } else if (id == 78) {
                if (userStore.userInfo.level == 3) {
                    window.open(detail.value.exp_url);
                    // openUriWithInputTimeoutHack('shoudszr://', () => {
                    //     ElMessageBox.confirm('请先下载安装客户端，再进行体验',
                    //         {
                    //             "show-close": false,
                    //             closeOnPressEscape: false,
                    //             autofocus: false,
                    //             confirmButtonText: "点击下载",
                    //         })
                    //         .then(() => {
                    //             window.open('https://yzpt-resources.oss-cn-hangzhou.aliyuncs.com/web/AI问诊.exe')
                    //             done()
                    //         })
                    //         .catch(() => {
                    //         })
                    // }, () => {
                    // })
                } else {
                    CaseContactDialogRef.value.show();
                }
            } else {
                if (detail.value.exp_url) {
                    window.open(detail.value.exp_url);
                } else {
                    CaseContactDialogRef.value.show();
                }
            }
        } else {
            LoginModal();
        }
    };

    const GetDetail = async () => {
        const res = await GetCateDetail({ id });
        if (res.code == 200) {
            detail.value = res.data;
        }
    };

    const getRecommendCase = async (size) => {
        const res = await GetRecommendCase({ size });
        if (res.code == 200) {
            recommend.value = res.data;
        }
    };

    const setView = async () => {
        const res = await SetExamcaseView({ id });
        detail.value.page_view = res.data;
    };

    if (userStore.isLogin && process.env.NODE_ENV === "production") {
        setView();
    }

    if (!userStore.isLogin) {
        if (pageStore.isMobile) {
            Dialog({
                title: "提示",
                message: "请先登录",
                "confirm-button-text": "去登录",
                "cancel-button-text": "返回",
                closeOnClickOverlay: false,
                teleport: "body",
                overlayStyle: {
                    "backdrop-filter": "blur(10px)",
                },
            })
                .then((type) => {
                    if (type === "confirm") {
                        navigateTo("/login.html");
                    } else if (type === "cancel") {
                        router.back();
                    }
                })
                .catch(() => { });
        } else {
            isLoginTipsDialogVisible.value = true;
        }
    }

    onMounted(async () => {
        await nextTick();

        setTimeout(() => {
            SetTaskScore({
                code: 1012,
                _id: id,
            });
        }, 5000);
    });

    return {
        detail,
        recommend,
        dataCaseList,
        CaseListDialogRef,
        CaseContactDialogRef,
        CaseMenusDialogRef,
        isLoginTipsDialogVisible,

        GetDetail,
        onCaseView,
        onCaseExperience,
        getRecommendCase,
    };
};
